#coding=utf-8
#Boa:Dialog:DlgKeyword

import wx
from MyListCtrl import MyKeywordListCtrl
from myLogger import *
from product import CDataKeyword

def create(parent):
    return DlgKeyword(parent)

[wxID_DLGKEYWORD, wxID_DLGKEYWORDBTNCANCEL, wxID_DLGKEYWORDBTNOK, 
] = [wx.NewId() for _init_ctrls in range(3)]

class DlgKeyword(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGKEYWORD, name=u'DlgKeyword',
              parent=prnt, pos=wx.Point(383, 287), size=wx.Size(513, 431),
              style=0, title=u'\u5173\u952e\u5b57\u8bbe\u7f6e')
        self.SetClientSize(wx.Size(505, 397))

        self.btnOK = wx.Button(id=wxID_DLGKEYWORDBTNOK, label=u'\u786e\u5b9a',
              name=u'btnOK', parent=self, pos=wx.Point(384, 136),
              size=wx.Size(75, 24), style=0)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnBtnOKButton,
              id=wxID_DLGKEYWORDBTNOK)

        self.btnCancel = wx.Button(id=wxID_DLGKEYWORDBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(384, 184), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGKEYWORDBTNCANCEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.myInit()

    def myInit(self):
        self.keywordList = MyKeywordListCtrl(self)
        self.keywordList.SetSize(wx.Size(350, 350))
        self.keywordList.Move(wx.Point(10, 10))

        self.curItem = -1
        
        self.keywordList.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        self.keywordList.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
        self.keywordList.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
    
    def setData(self, keywordObjList):
        pass
    
    def OnItemSelected(self,event):
        '''项目选中时，标记项目，以便左键双击时获取项目
            需注意：Ctrl+left选中多项，不会改变取消之前选中项
        '''
        self.curItem = event.m_itemIndex
    
    def OnItemDeselected(self, evt):
        item = evt.GetItem()
        if(self.curItem == item):
            self.curItem = -1
                
    def OnLeftDClick(self,event):
        if (self.curItem == -1):
            return
        
        self.keywordSel = []    
        name = self.keywordList.GetItemText(self.curItem)
        self.keywordSel.append(CDataKeyword.getFirstInstance({CDataKeyword.COL_NAME:name}))
        self.EndModal(wx.ID_OK)
        
    def OnBtnOKButton(self, event):
        self.keywordSel = []
        item = -1
        
        cnt = self.keywordList.GetSelectedItemCount()
##        if (cnt == 0):
##            dlg = wx.MessageDialog(self, u'尚未选择关键字，是否继续退出',u'提示',wx.YES_NO|wx.ICON_QUESTION)
##            if (dlg.ShowModal() == wx.ID_YES):
##                self.EndModal(wx.ID_OK)
##            return
            
        for i in range(cnt):
            item = self.keywordList.GetNextItem(item, state = wx.LIST_STATE_SELECTED)
            if item == -1:
                log(LOG_ERR, 'i=%d cnt=%d', i, self.keywordList.GetSelectedItemCount())
                return
            
            name = self.keywordList.GetItemText(item)
            self.keywordSel.append(CDataKeyword.getFirstInstance({CDataKeyword.COL_NAME:name}))
            log(LOG_DBG, 'item=%d text=%s', (item, self.keywordList.GetItemText(item)))
        
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

        
